/**
 * 异步组件加载 create to by doujiabao - 20191210
 */

import React, { Component } from 'react'

import './index.css'
import 'nprogress/nprogress.css'

import componentProgress from 'nprogress'

componentProgress.configure({ showSpinner: false })

export default function asyncComponent(importComponent) {
    class AsyncComponent extends Component {

        state = {
            component: null
        }

        async componentDidMount() {

            componentProgress.start()

            const { default: component } = await importComponent()

            componentProgress.done()

            this.setState({ component })

        }

        render() {
            const Component = this.state.component

            return Component ? <Component {...this.props} /> : null
        }

    }

    return AsyncComponent
}


